package com.gimi.cloud.jimi.app.consumer.common;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.DateUtil;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tk.mybatis.mapper.util.StringUtil;



public class CheckUtil {
	 private static Logger logger = LoggerFactory.getLogger(CheckUtil.class);
	/**
	 * 校验用户ID
	 * 
	 * @param userId
	 * @param httpServletResponse
	 */
	public static void checkUserId(String userId, HttpServletResponse httpServletResponse) {
		Response resp = new Response();
		if (StringUtil.isEmpty(userId)) {
			logger.error("入参校验，参数错误,无效的用户ID");
			resp.setResponseCode(ResponseCodeEnum.ERR_USER_INVALID);
			Util.sendResponse(JsonUtil.toJson(resp), httpServletResponse);
			return;
		}
		long user_id;
		try {
			user_id = Long.parseLong(userId);
		} catch (NumberFormatException e) {
			logger.error("入参校验，参数错误,无效的用户ID");
			resp.setResponseCode(ResponseCodeEnum.ERR_USER_INVALID);
			Util.sendResponse(JsonUtil.toJson(resp), httpServletResponse);
			return;
		}
	}
	
	
	/**
	 * 校验是否为有效手机号
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		boolean flag = false;
		try {
			// Pattern p = Pattern
			// .compile("^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$");
			Pattern p = Pattern.compile("^1[3456789]\\d{9}$");
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 校验身份证时间有效时间
	 * 
	 * @param valid_date
	 *            有效时间
	 * @return null 参数正确
	 */
	public static void checkValidDate(String valid_date, HttpServletResponse httpServletResponse) {
		// 检查是否为空
		if (StringUtil.isEmpty(valid_date)) {
			logger.error("入参校验，参数错误,身份证有效期为空");
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.EMPTY_RESULTSET);
			Util.sendResponse(JsonUtil.toJson(resp), httpServletResponse);
			return;
		}
		String[] dates = valid_date.split("-");
		// 检查格式
		if (dates ==null || dates.length != 2 || !isNumeric(dates[0]) || !isNumeric(dates[1])) {
			logger.error("入参校验，参数错误,身份证有效期格式错误");
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.PARAMS_FORMAT_ERROR);
			Util.sendResponse(JsonUtil.toJson(resp), httpServletResponse);
			return;
		
		}
		// 获取身份证到期时间
		String endTime = dates[1];
		// 判断是否过期
		if (Long.valueOf(endTime) < Long.valueOf(DateUtil.dateStr7(new Date()))) {
			// 过期
			logger.error("入参校验，参数错误,身份证有效期格式错误");
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.ID_CARD_EXPIRED);
			Util.sendResponse(JsonUtil.toJson(resp), httpServletResponse);
			return;
		} 
	}

	/**
	 * 匹配字符串是否全是数字
	 * @param str
	 * @return
	 */
	private static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
}
